package com.kaibes.core.base.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

public class PropertyUtils {

    public static ResourceBundle getDefaultResourceBundle() {
        return getResourceBundle("application");
    }
    
    public static ResourceBundle getResourceBundle(String name, Locale locale) {
        return ResourceBundle.getBundle(name, locale);
    }
    
    public static ResourceBundle getResourceBundle(String name) {
        return getResourceBundle(name, Locale.getDefault());
    }
    
    public static Properties getProperties(InputStream is) throws IOException {
        Properties properties = new Properties();
        properties.load(is);
        is.close();
        return properties;
    }
    
    public static Properties getProperties(String path) {
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream(path)) {
            properties.load(fis);
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
    
    public static void saveProperties(Properties properties, String path) {
        try (FileOutputStream fos = new FileOutputStream(path)) {
            properties.store(fos, "update on "+LocalDate.now().toString());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
